home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-28 | 8.8 KB | 374 lines | [TEXT/CWIE] |
- // File "sample win.c" -
-
- #include "main.h"
- #include "floaters.h"
- #include "prefs.h"
- #include "sample drag.h"
- #include "sample win.h"
- #include <string.h>
- #include "Offscreen.h"
-
- // ***********************************************************************************
- // Global Declarations
-
- extern GlobalsRec glob;
- WindowPtr gAdWindow = nil;
- void ShowSponsorship(void);
- WindowPtr gSplashWindow;
-
- void ShowSplash(StringPtr name);
- void HideSplash(void);
- void SplashIdle();
-
- // ***********************************************************************************
- // ***********************************************************************************
-
- WindowPtr NewSampleWindow() {
- static short count = 0, **prefCount;
- Rect bounds;
- WindowPtr win;
- Str255 textBuff = "\pSample Window ", altBuff;
- StringPtr winTextPtr;
-
- // Demo the prefs functions by incrementing the Window #
- if (prefCount = (short **) GetPrefs('WCnt', 1)) {
- count = (**prefCount < 999) ? ++(**prefCount) : (**prefCount = 0);
- WritePrefs((Handle) prefCount, 'WCnt', 1);
- DisposeHandle((Handle) prefCount);
- }
- else if (prefCount = (short **) NewHandle(sizeof(**prefCount))) {
- **prefCount = ++count;
- WritePrefs((Handle) prefCount, 'WCnt', 1);
- DisposeHandle((Handle) prefCount);
- }
- else count++;
-
- // Setup the Window Title String
- NumToString(count, altBuff);
- BlockMove(altBuff+1, textBuff + textBuff[0] + 1, altBuff[0]);
- textBuff[0] += altBuff[0];
-
-
- bounds = qd.screenBits.bounds;
- //SetRect(&bounds, 100, 100, 320, 202);
- //OffsetRect(&bounds, (count-1) % 3 * 40, (count-1) % 3 * 40);
- gAdWindow = win = NewFloater(0, &bounds, textBuff, FALSE, kWDEFProcID, (WindowPtr) -1, TRUE, 0, '*923',
- SampleWindowEventHandler, DisposeSampleWindow);
-
- // Apply some text to draw for update routines
- if (win) SetSampleWindowText(win,"\p");
-
- if (win) {
- UpdateFloater(win); // Force an immediate draw
- // if (glob.hasDragMgr) SetupDragHandlers(win);
- }
-
- SetRect(&bounds, 100, 100, 400, 250);
- gSplashWindow = win = NewFloater(0, &bounds, textBuff, FALSE, plainDBox, (WindowPtr) -1, TRUE, 0, '*923',
- SampleWindowEventHandler, DisposeSampleWindow);
-
-
- return(win);
- }
-
- // ***********************************************************************************
- // ***********************************************************************************
-
- void DisposeSampleWindow(WindowPtr win) {
- StringPtr winTextPtr;
-
- if (winTextPtr = (StringPtr) GetWRefCon(win)) DisposePtr((Ptr) winTextPtr);
- DisposeFloater(win);
-
- // For a background only app, this will cause us to quit when the user
- // closes the last floater. Its your choice if you want this to happen.
- if (glob.bkgdOnly && ! GetIndFloater(1, FALSE)) glob.quitting = TRUE;
- }
-
- // ***********************************************************************************
- // ***********************************************************************************
-
- void SampleWindowEventHandler(EventRecord *theEvent, WindowPtr win) {
- Rect destRect;
- GrafPtr savePort;
- StringPtr winTextPtr;
-
- switch(theEvent->what) {
- case nullEvent:
- break;
- case mouseDown: {
- // The Window part has already been found and forwarded to us
- short thePart = theEvent->message;
-
- switch(thePart) {
- case inContent:
- GetPort(&savePort);
- SetPort(win);
-
- if (glob.hasColorQD)
- LMSetHiliteMode(LMGetHiliteMode() ^ (1L << hiliteBit));
- InvertRect(&win->portRect);
-
- if (glob.hasDragMgr) SampleWindowDrag(theEvent, win);
- else while(StillDown());
-
- if (glob.hasColorQD)
- LMSetHiliteMode(LMGetHiliteMode() ^ (1L << hiliteBit));
- InvertRect(&win->portRect);
-
- SetPort(savePort);
- break;
- case inGrow:
- break;
- case inGoAway:
- if (TrackGoAway(win, theEvent->where))
- DisposeSampleWindow(win);
- break;
- case inZoomIn:
- break;
- case inZoomOut:
- break;
- }
- break;
- }
- case keyDown:
- case autoKey:
- break;
- case updateEvt: {
- GetPort(&savePort);
- SetPort(win);
- BeginUpdate(win);
-
- // Draw something cool!
- destRect = win->portRect;
- EraseRect(&destRect);
- InsetRect(&destRect, 3, 2);
- TextFont(1); TextSize(9);
- if (winTextPtr = GetSampleWindowText(win))
- TextBox(winTextPtr+1, winTextPtr[0], &destRect, teFlushDefault);
- TextFont(0); TextSize(0);
-
- EndUpdate(win);
- SetPort(savePort);
- break;
- }
- }
- }
-
- // ***********************************************************************************
- // ***********************************************************************************
-
- void SetSampleWindowText(WindowPtr win, StringPtr winText) {
- Boolean dirty = FALSE;
- Ptr storage = (Ptr) GetWRefCon(win);
- GrafPtr savePort;
-
- if (! storage) { storage = NewPtr(sizeof(Str255)); dirty = TRUE; }
- if (! storage) return;
-
- if (winText && ! EqualString(winText, (StringPtr) storage, 0, 0)) dirty = TRUE;
- BlockMove((winText && winText[0]) ? winText : "\p", storage, sizeof(Str255));
- SetWRefCon(win, (long) storage);
-
- // Force a redraw after changing content
- if (dirty) {
- GetPort(&savePort);
- SetPort(win);
- InvalRect(&win->portRect);
- SetPort(savePort);
- }
- }
-
-
- static short Rand(short range)
- {
- return (Random () & 0x7fff) % range;
- }; /*Rand*/
-
- Str255 message = "\pBrought to you by:";
-
- void ShowSponsorship(void)
- {
- GrafPtr savePort;
- short countRes;
-
- GetPort(&savePort);
- if(gAdWindow)
- {
- RGBColor color = {0xffff, 0, 0};
- Rect drawRect, picRect;
- WindowOffscreen *offscreen;
- PicHandle pic;
-
- ShowWindow(gAdWindow);
- SetPort(gAdWindow);
-
- offscreen = DrawOffscreen( nil, gAdWindow);
- TextFont(helvetica);
- TextSize(36);
- TextFace(bold);
- RGBForeColor(&color);
-
- drawRect = gAdWindow->portRect;
-
- drawRect.top += 25;
- TETextBox( &(message[1]), message[0], &drawRect, teCenter);
-
- countRes = Count1Resources('PICT');
-
- pic = (PicHandle)Get1IndResource('PICT', Rand(countRes) + 1);
-
- if(pic)
- {
- short top, left;
-
- picRect = (*pic)->picFrame;
-
- OffsetRect(&picRect, -picRect.left, -picRect.top);
-
- top = (gAdWindow->portRect.bottom - 72 - picRect.bottom);
- top /= 2;
- top += 72;
-
- left = gAdWindow->portRect.right - picRect.right;
- left /= 2;
-
- OffsetRect(&picRect, left, top);
-
- DrawPicture(pic, &picRect);
- }
-
- DrawOnscreen(offscreen);
-
- {
- long lastTick = TickCount() + 300;
-
- while(lastTick > TickCount())
- {
-
- }
- }
- HideWindow(gAdWindow);
- }
- SetPort(savePort);
- }
-
-
- static void CenterWindow(WindowPtr w, int top)
- {
- Rect scr; Point p;
- int rsize,size,margin,xoff,yoff;
- GrafPtr wMgrPort;
-
-
- GetWMgrPort(&wMgrPort);
- scr = wMgrPort->portRect;
- SetPort(w);
- p.h = w->portRect.left; p.v = w->portRect.top;
- LocalToGlobal(&p);
-
- size = scr.right - scr.left;
- rsize = w->portRect.right - w->portRect.left;
- margin = size - rsize;
- if (margin > 0) {
- margin >>= 1;
- p.h = scr.left + margin;
- }
- size = scr.bottom - scr.top;
- rsize = w->portRect.bottom - w->portRect.top;
- margin = size - rsize;
- if (margin > 0) {
- margin >>= 1;
- p.v = scr.top + margin;
- }
- MoveWindow(w,p.h,top?top:p.v,FALSE);
-
- }
-
- PicHandle gPicture = nil;
- Boolean gUsedPicture = false;
- short gPicIndex = 1;
- void SplashIdle()
- {
- if(gUsedPicture || gPicture == nil)
- {
- if(gPicture) DisposeHandle((Handle)gPicture);
- gPicture = nil;
-
- gPicture = (PicHandle)Get1IndResource('PICT', gPicIndex++);
-
- if(!gPicture)
- {
- gPicIndex = 1;
- gPicture = (PicHandle)Get1IndResource('PICT', gPicIndex++);
- }
-
- if(gPicture)
- DetachResource((Handle)gPicture);
-
- gUsedPicture = false;
- }
- }
-
- void ShowSplash(StringPtr name)
- {
- GrafPtr savePort;
- Rect textRect, picRect;
- Str255 splashMessage = "\pSponsored by:";
-
- GetPort(&savePort);
- textRect = gSplashWindow->portRect;
-
- if(gPicture)
- {
- picRect = (*gPicture)->picFrame;
- OffsetRect(&picRect, -picRect.left, -picRect.top);
- textRect.bottom = textRect.top + 24 + (picRect.bottom - picRect.top);
- textRect.right = textRect.left + (picRect.right - picRect.left);
-
- SizeWindow(gSplashWindow,textRect.right,textRect.bottom,false);
- }
- CenterWindow(gSplashWindow, 0);
- textRect = gSplashWindow->portRect;
-
- gUsedPicture = true;
-
- ShowWindow(gSplashWindow);
- SetPort(gSplashWindow);
-
- if(gPicture)
- {
- OffsetRect(&picRect, 0, 24);
- DrawPicture(gPicture, &picRect);
- }
-
- textRect.bottom = textRect.top + 12;
- TextFont(applFont);
- TextSize(10);
- TextFace(bold);
-
- TETextBox(&(name[1]), name[0], &textRect, teCenter);
-
- OffsetRect(&textRect, 0, 12);
- TETextBox(&(splashMessage[1]), splashMessage[0], &textRect, teCenter);
-
- SetPort(savePort);
-
- }
-
- void HideSplash(void)
- {
- HideWindow(gSplashWindow);
- }
-
- // ***********************************************************************************
- // ***********************************************************************************
-
- StringPtr GetSampleWindowText(WindowPtr win) {
- Ptr storage = (Ptr) GetWRefCon(win);
- StringPtr winText;
-
- winText = (storage && ((StringPtr) storage[0])) ? ((StringPtr) storage) : "\p<No Data>";
- return(winText);
- }
-